#!/bin/bash

network_id=$1

function check_response {
  url="$1"
  response=$(curl -s -o /dev/null -w "%{http_code}" "$url")
  if [ "$response" == "200" ]; then
    return 0
  fi
  return 1
}

ping_url="http://localhost:808$network_id/ping"
echo "Waiting for 'pong' from $ping_url..."
for i in {1..10}; do
    if check_response "$ping_url"; then
        echo "Received 200 OK from $ping_url"
        break
    fi
    sleep 1
    echo "($i) retrying..."
done

container_name="custom-dind-$network_id"
# 初始化 10 个用户
for i in {1..40}; do
    echo "Creating user $i in $container_name"
    address=$(printf '%040d' $i)
    echo "Using address: $address, network ID: $network_id"
    echo "URL: http://localhost:808$network_id/assert/mint?chainId=chain1"
    
    response=$(curl -X POST "http://localhost:808$network_id/assert/mint?chainId=chain1" \
    -H 'Content-Type: application/json' \
    -d '{
        "amount": 100000000,
        "tokenID": "TEST_TOKEN",
        "clientID": "'"$address"'"
    }')
    
    if [ $? -ne 0 ]; then
        echo "Failed to create user $i in $container_name"
    else
        echo "User $i created successfully in $container_name"
    fi
done
